﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace DotnetSpider.Lawyee.Src
{
	class RedisHelpers
	{

		public static long StringIncrement(string incrKey)
		{
			return RedisClient.Database.StringIncrement(incrKey);
		}

		public static IEnumerable<string> SetMembers(string key)
		{
			IEnumerable<string> list = RedisClient.Database.SetMembers(key).Select(p => p.ToString());
			return list;
		}

		public static bool HashExists(string key, string field)
		{
			bool result = RedisClient.Database.HashExists(key, field);
			return result;
		}

		public static Dictionary<string, string> HashGetAll(string key)
		{
			Dictionary<string, string> dict = RedisClient.Database.HashGetAll(key).ToDictionary(k => k.Name.ToString(), v => v.Value.ToString());
			return dict;
		}

		public static int HashLength(string key)
		{
			int result = (int)RedisClient.Database.HashLength(key);
			return result;
		}

		public static long HashIncrement(string key, string field, long money)
		{
			return RedisClient.Database.HashIncrement(key, field, money);
		}

		public static long StringIncrement(string key, TimeSpan expires, long value = 1)
		{
			var result = RedisClient.Database.StringIncrement(key, value);
			var exp = RedisClient.Database.KeyExpire(key, expires);
			return result;
		}

		public static bool ListLeftPush<T>(string key, T data) where T : class
		{
			if (data == null)
				return false;

			string value = JsonConvert.SerializeObject(data);
			return RedisClient.Database.ListLeftPush(key, value) > 0;
		}

		public static T ListRightPop<T>(string key) where T : class
		{
			string data = RedisClient.Database.ListRightPop(key);
			if (string.IsNullOrWhiteSpace(data))
				return null;

			T item = JsonConvert.DeserializeObject<T>(data);
			return item;
		}

		public static bool HashSet(string key, string field, string value)
		{
			return RedisClient.Database.HashSet(key, field, value);
		}

		public static bool HashSet(string key, string field, string value, TimeSpan expires)
		{
			var set = RedisClient.Database.HashSet(key, field, value);
			var exp = RedisClient.Database.KeyExpire(key, expires);
			return set && exp;
		}

		public static string HashGet(string key, string field)
		{
			return RedisClient.Database.HashGet(key, field);
		}

		public static bool HashDelete(string key, string field)
		{
			return RedisClient.Database.HashDelete(key, field);
		}

		public static bool StringSet<T>(string key, T data)
		{
			if (data == null)
				return false;

			string value = JsonConvert.SerializeObject(data);
			return RedisClient.Database.StringSet(key, value);
		}

		public static bool StringSet<T>(string key, T data, TimeSpan expires)
		{
			if (data == null)
				return false;

			string value = JsonConvert.SerializeObject(data);
			return RedisClient.Database.StringSet(key, value, expires);
		}

		public static T StringGet<T>(string key)
		{
			string value = RedisClient.Database.StringGet(key);
			if (string.IsNullOrWhiteSpace(value))
			{
				object obj = null;

				switch (typeof(T).FullName)
				{
					case "System.Int32":
						obj = 0;
						break;
					default:
						obj = null;
						break;
				}

				return (T)obj;
			}

			T item = JsonConvert.DeserializeObject<T>(value);
			return item;
		}

		public static bool KeyDelete(string key)
		{
			return RedisClient.Database.KeyDelete(key);
		}
	}
}
